{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pandas 使用 Series()  函数来创建 Series 对象，通过这个对象可以调用相应的方法和属性，从而达到处理数据的目的：\n",
    "\n",
    "参数名称 | 描述\n",
    " :-: | :-: \n",
    "data | 输入的数据，可以是列表、常量、ndarray 数组等 \n",
    "index | 索引值必须是惟一的，如果没有传递索引，则默认为 np.arrange(n) \n",
    "dtype | dtype表示数据类型，如果没有提供，则会自动判断得出\n",
    "copy | 表示对 data 进行拷贝，默认为 False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一列   -210.875000\n",
      "第二列    -24.734375\n",
      "第二列      0.997559\n",
      "dtype: float16\n"
     ]
    }
   ],
   "source": [
    "# 创建序列\n",
    "array = np.random.randn(3)*100\n",
    "series = pd.Series(array,index=[\"第一列\",\"第二列\",\"第二列\"],dtype=\"float16\")\n",
    "print(series)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以把 dict 作为输入数据。如果没有传入索引时会按照字典的键来构造索引；反之，当传递了索引时需要将索引标签与字典中的值一一对应。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一列    0.0\n",
      "第二列    1.0\n",
      "第三列    2.0\n",
      "dtype: float16\n"
     ]
    }
   ],
   "source": [
    "# 字典创建序列\n",
    "data = {'第一列' : 0., '第二列' : 1., '第三列' : 2.}\n",
    "series = pd.Series(data,dtype=\"float16\")\n",
    "print(series)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Series 对象的常用属性:\n",
    "\n",
    "名称|属性\n",
    ":-|:-\n",
    "axes|以列表的形式返回所有行索引标签。\n",
    "dtype|返回对象的数据类型。\n",
    "empty|返回一个空的 Series 对象。\n",
    "ndim|返回输入数据的维数。\n",
    "size|返回输入数据的元素数量。\n",
    "values|以 ndarray 的形式返回 Series 对象。\n",
    "index|返回一个RangeIndex对象，用来描述索引的取值范围。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Index(['第一列', '第二列', '第三列'], dtype='object')]\n",
      "---------------------------------------------------------------------\n",
      "float16\n",
      "---------------------------------------------------------------------\n",
      "1\n",
      "---------------------------------------------------------------------\n",
      "3\n",
      "---------------------------------------------------------------------\n",
      "Index(['第一列', '第二列', '第三列'], dtype='object')\n",
      "---------------------------------------------------------------------\n",
      "[0. 1. 2.]\n",
      "---------------------------------------------------------------------\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(series.axes)\n",
    "print(\"---------------------------------------------------------------------\")\n",
    "\n",
    "print(series.dtype)\n",
    "print(\"---------------------------------------------------------------------\")\n",
    "\n",
    "print(series.ndim)\n",
    "print(\"---------------------------------------------------------------------\")\n",
    "\n",
    "print(series.size)\n",
    "print(\"---------------------------------------------------------------------\")\n",
    "\n",
    "print(series.index)\n",
    "print(\"---------------------------------------------------------------------\")\n",
    "\n",
    "print(series.values)\n",
    "print(\"---------------------------------------------------------------------\")\n",
    "\n",
    "print(series.empty)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1、series.head(n) 返回前 n 行数据，默认显示前 5 行数据。  \n",
    "2、series.tail(n) 返回后 n 行数据，默认显示后 5 行数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一列    0.0\n",
      "第二列    1.0\n",
      "dtype: float16\n",
      "---------------------------------------------------------------------\n",
      "第二列    1.0\n",
      "第三列    2.0\n",
      "dtype: float16\n"
     ]
    }
   ],
   "source": [
    "print(series.head(2))\n",
    "print(\"---------------------------------------------------------------------\")\n",
    "print(series.tail(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一列    0.0\n",
      "dtype: float16\n"
     ]
    }
   ],
   "source": [
    "# 切片\n",
    "print(series[0:1])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
